package com.hzb.erp.api.pc.course.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hzb.erp.api.pc.clazz.entity.ClassGrade;
import com.hzb.erp.api.pc.clazz.service.ClassGradeService;
import com.hzb.erp.api.pc.course.entity.Course;
import com.hzb.erp.api.pc.course.entity.CourseImage;
import com.hzb.erp.api.pc.course.entity.CourseLink;
import com.hzb.erp.api.pc.course.mapper.CourseImageMapper;
import com.hzb.erp.api.pc.course.mapper.CourseLinkMapper;
import com.hzb.erp.api.pc.course.mapper.CourseMapper;
import com.hzb.erp.api.pc.course.pojo.CourseParamDTO;
import com.hzb.erp.api.pc.course.pojo.CourseSaveDTO;
import com.hzb.erp.api.pc.course.pojo.CourseVO;
import com.hzb.erp.api.pc.course.service.CourseService;
import com.hzb.erp.common.enums.SwitchEnum;
import com.hzb.erp.exception.BizException;
import com.hzb.erp.sysservice.ImportExportService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author 541720500@qq.com
 */
@Service
public class CourseServiceImpl extends ServiceImpl<CourseMapper, Course> implements CourseService {

    @Resource
    private CourseLinkMapper courseLinkMapper;

    @Resource
    private CourseImageMapper courseImageMapper;
    @Resource
    private ClassGradeService classGradeService;
    @Autowired
    private ImportExportService importExportService;

    @Override
    public CourseVO getInfo(Long id) {
        return this.baseMapper.getInfo(id, null);
    }

    @Override
    public CourseVO getInfoByName(String name) {
        return this.baseMapper.getInfo(null, name);
    }

    @Override
    public IPage<CourseVO> getList(CourseParamDTO param) {
        return this.baseMapper.getList(new Page<>(param.getPage(), param.getPageSize()), param);
    }

    @Override
    public List<CourseVO> getAll(CourseParamDTO param) {
        return this.baseMapper.getList(param);
    }

    @Override
    public Course getByName(String name) {
        QueryWrapper<Course> qw = new QueryWrapper<>();
        qw.eq("name", name);
        return this.getOne(qw);
    }

    @Override
    public Boolean saveOrUpdateByDTO(CourseSaveDTO courseSaveDTO) {
        Course item = new Course();

        if (courseSaveDTO.getName() != null) {
            Course find = this.getByName(courseSaveDTO.getName());
            if (find != null && !find.getId().equals(courseSaveDTO.getId())) {
                throw new BizException("该课程名已存在");
            }
        }

        BeanUtils.copyProperties(courseSaveDTO, item);

        if ("期".equals(courseSaveDTO.getUnitName())) {
            BigDecimal res = courseSaveDTO.getPrice().divide(BigDecimal.valueOf(courseSaveDTO.getLessonCount()), RoundingMode.HALF_UP);
            if (res.compareTo(new BigDecimal(1)) < 0) {
                throw new BizException("单价不能小于1");
            }
            item.setUnitPrice(res);
        }

        if(CollectionUtils.isNotEmpty(courseSaveDTO.getGradeIds())) {
            item.setGradeIds(StringUtils.join(courseSaveDTO.getGradeIds(), ","));
            List<ClassGrade> classGrades = classGradeService.listByIds( courseSaveDTO.getGradeIds());
            item.setGradeNames(StringUtils.join(classGrades.stream().map(ClassGrade::getName).collect(Collectors.toList()), ","));
        }

        boolean res = this.saveOrUpdate(item);
        QueryWrapper<CourseImage> qw = new QueryWrapper<>();
        qw.eq("course_id", item.getId());
        courseImageMapper.delete(qw);

        for(String url: courseSaveDTO.getImages()) {
            CourseImage img = new CourseImage();
            img.setCourseId(item.getId());
            img.setImageUrl(url);
            courseImageMapper.insert(img);
        }

        return res;
    }

    @Override
    public Boolean delete(List<Long> ids) {
        return this.removeByIds(ids);
    }

    @Override
    public Boolean open(List<Long> ids) {

        List<Course> list = this.listByIds(ids);
        for (Course item : list) {
            item.setState(SwitchEnum.YES);
        }
        return this.updateBatchById(list);
    }

    @Override
    public Boolean close(List<Long> ids) {
        List<Course> list = this.listByIds(ids);
        for (Course item : list) {
            item.setState(SwitchEnum.NO);
        }
        return this.updateBatchById(list);
    }

    @Override
    public Integer handleLink(Long courseId, Long[] linkedIds) {
        handleUnlink(courseId, linkedIds);
        int count = 0;
        for (int i = 0; i < linkedIds.length; i++) {
            CourseLink item = new CourseLink();
            item.setCourseId(courseId);
            item.setLinkedId(linkedIds[i]);
            courseLinkMapper.insert(item);
            count = i;
        }
        return count + 1;
    }

    @Override
    public Integer handleUnlink(Long courseId, Long[] linkedIds) {
        int count = 0;
        for (int i = 0; i < linkedIds.length; i++) {
            Map<String, Object> map = new HashMap<>(2);
            map.put("course_id", courseId);
            map.put("linked_id", linkedIds[i]);
            courseLinkMapper.deleteByMap(map);
            count = i;
        }
        return count + 1;
    }

    /**
    * 导出
    * */
    @Override
    public void exportData(CourseParamDTO param) {
        List<CourseVO> list = this.baseMapper.getList(param);
        LinkedHashMap<String, String> header = new LinkedHashMap<String, String>() {{
            put("id", "ID");
            put("name", "课程名称");
            put("subjectName", "科目");
            put("price", "价格");
            put("unitPrice", "单次课价");
            put("lessonCount", "课时数");
            put("lessonType", "班级类型");
        }};
        importExportService.exportExcel(header, list, "课程数据列表");
    }

}
